package com.dlc.beijingrfid.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import com.dlc.beijingrfid.R;
import com.dlc.beijingrfid.adapter.CheckDetailsAdapter;
import com.dlc.beijingrfid.base.BaseActivity;
import com.dlc.beijingrfid.net.bean.BackBean;
import com.dlc.beijingrfid.net.bean.OrderDetailBean;
import com.dlc.beijingrfid.net.bean.OrderRfidsBean;
import com.dlc.beijingrfid.net.bean.RFIDListBean;
import com.dlc.beijingrfid.net.bean.SimpleBean;
import com.dlc.beijingrfid.net.bean.StockCheckData;
import com.dlc.beijingrfid.net.exception.ErrorMsgException;
import com.dlc.beijingrfid.net.protocol.MyProto;
import com.dlc.beijingrfid.net.protocol.StockProto;
import com.dlc.beijingrfid.net.protocol.base.HttpProtocol;
import com.dlc.commonlibrary.dialog.SureDialog;
import com.dlc.commonlibrary.widget.TitleBar;
import com.trello.rxlifecycle.android.ActivityEvent;
import java.util.ArrayList;
import java.util.List;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017-07-26.
 */

public class BuCaoResultActivity extends BaseActivity {
    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.btn_sure)
    Button btn_sure;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.order_num)
    TextView orderNum;
    @BindView(R.id.real_num)
    TextView realNum;
    @BindView(R.id.reduce_num)
    TextView reduceNum;
    @BindView(R.id.add_num)
    TextView addNum;
    //private BucaoResultAdapter mBucaoResultAdapter;
    private CheckDetailsAdapter mCheckDetailsAdapter;

    private int page = 1;
    private String pagesize = "100000";
    private String orderno;
    private String rfid = "";

    //ArrayList<String> reduceList = new ArrayList<>();//库存减少的数据
    // ArrayList<String> addList = new ArrayList<>();//扫描增加的数据
    private ArrayList<String> RfList = new ArrayList<>();//手持机读取的所有rfid
    private ArrayList<String> dataList = new ArrayList<>();//扫描订单二维码获取到的所有rfid

    List<RFIDListBean.DataBean.ListBean> list = new ArrayList<>();

    private List<StockCheckData> mStockCheckList = new ArrayList<>();
    private int exisNumber = 0;//存在仓库的数据
    private int NoEnvironment = 0;//非环境中的rfid
    private String id = "";

    @Override
    protected int getLayoutID() {
        return R.layout.activity_bucao_result;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleBar.leftImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (exisNumber != NoEnvironment){
                    SureDialog sureDialog =
                        new SureDialog(BuCaoResultActivity.this, getString(R.string.submit_bucao));
                    sureDialog.setOnCallBackListener(new SureDialog.OnCallBack() {
                        @Override
                        public void sure(){
                            if (!TextUtils.isEmpty(orderno) && orderno.contains("W")) {
                                washTransfer();
                            } else if (!TextUtils.isEmpty(orderno) && orderno.contains("D")) {
                                deliveryTransfer();
                            }
                        }

                        @Override
                        public void cancle() {

                        }
                    });
                }else {
                    if (!TextUtils.isEmpty(orderno) && orderno.contains("W")) {
                        washTransfer();
                    } else if (!TextUtils.isEmpty(orderno) && orderno.contains("D")) {
                        deliveryTransfer();
                    }
                }
            }
        });

        // reduceList = getIntent().getStringArrayListExtra("reduceList");
        // addList = getIntent().getStringArrayListExtra("addList");
        dataList = getIntent().getStringArrayListExtra("dataList");
        RfList = getIntent().getStringArrayListExtra("RfList");
        orderno = getIntent().getStringExtra("orderno");
        id = getIntent().getStringExtra("id");

        orderNum.setText(String.valueOf(dataList.size()));
        realNum.setText(String.valueOf(RfList.size()));
        // reduceNum.setText(String.valueOf(reduceList.size()));
        // addNum.setText(String.valueOf(addList.size()));


        for (int i = 0; i < RfList.size(); i++) {
            rfid = rfid + RfList.get(i)+ ",";
        }
        if (!TextUtils.isEmpty(rfid))
            rfid = rfid.substring(0, rfid.length() - 1);
        else
            rfid = "-1";
        InitRecyclerView();
        //getOrderWashDetail();
        getData();

    }

    private void InitRecyclerView() {
        mCheckDetailsAdapter = new CheckDetailsAdapter();
        mCheckDetailsAdapter.addHeaderView( LayoutInflater.from(BuCaoResultActivity.this).inflate(R.layout.item_bucao_result_details, null, false));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mCheckDetailsAdapter);

    }


    private void getData(){
        String datarfid ="";
        for (int i = 0; i < dataList.size(); i++) {
            datarfid = datarfid + dataList.get(i)+ ",";
        }

        MyProto.get().order_rfids(rfid,orderno)
            .subscribeOn(Schedulers.io())
            .compose(this.<OrderRfidsBean>bindUntilEvent(ActivityEvent.DESTROY))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<OrderRfidsBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(OrderRfidsBean orderCheckBean) {
                    doStockData(orderCheckBean);
                }
            });
    }
    private void doStockData(OrderRfidsBean bean){
        mStockCheckList.clear();
        exisNumber = 0;
        NoEnvironment = 0;
        if (bean.data!= null&&bean.data.size()>0) {
            for (int i = 0;i < bean.data.size();i++){
                StockCheckData data = new StockCheckData(bean.data.get(i).title,bean.data.get(i).order_nums,bean.data.get(i).read_nums,
                    bean.data.get(i).hotel,bean.data.get(i).office);
                exisNumber = exisNumber + Integer.parseInt(bean.data.get(i).order_nums);
                if (TextUtils.isEmpty(bean.data.get(i).read_nums)){
                    NoEnvironment = NoEnvironment + 0;
                }else {
                    NoEnvironment = NoEnvironment + Integer.parseInt(bean.data.get(i).read_nums);
                }
                mStockCheckList.add(data);
            }
        }


        mCheckDetailsAdapter.setNewData(mStockCheckList);
    }
    /**
     * 送洗交接
     */
    public void washTransfer() {
        MyProto.get()
            .orderWashReceive(orderno, rfid)
            .subscribeOn(Schedulers.io())
            .compose(this.<SimpleBean>bindUntilEvent(ActivityEvent.DESTROY))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<SimpleBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    showOneToast(e.getMessage());
                }

                @Override
                public void onNext(SimpleBean bean) {
                    showToast("交接成功!");
                    finish();
                    startActivity(MainActivity.class);
                }
            });
    }

    /**
     * 送货交接
     */
    public void deliveryTransfer() {
        MyProto.get()
            .orderDeliverReceive(orderno, rfid)
            .subscribeOn(Schedulers.io())
            .compose(this.<BackBean>bindUntilEvent(ActivityEvent.DESTROY))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<BackBean>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    showOneToast(e.getMessage());
                }

                @Override
                public void onNext(BackBean bean) {
                    showToast("交接成功!");
                    getDetailData(bean.data.order_id+"");

                }
            });
    }
    private void getDetailData(String id) {
        StockProto.get()
            .getOrderDetail("order_deliver_detail", id,
                new HttpProtocol.Callback<OrderDetailBean>() {
                    @Override
                    public void onSuccess(OrderDetailBean orderDetailBean) {
                        dismissLoadingDialog();
                        Toast.makeText(getActivity(), orderDetailBean.msg, Toast.LENGTH_SHORT)
                            .show();
                        Intent intent = new Intent(getActivity(), OrderDetailActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("detailData", orderDetailBean);
                        intent.putExtras(bundle);
                        intent.putExtra("type", 1);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onFailed(int code, ErrorMsgException e) {
                        showOneToast(e.getMessage());
                        dismissLoadingDialog();
                        finish();
                    }
                });
    }
}
